package en.edu.zucc.pb.loginapplication.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import en.edu.zucc.pb.loginapplication.MainActivity;
import en.edu.zucc.pb.loginapplication.R;
import en.edu.zucc.pb.loginapplication.layout.KeyBoardLinearLayout;
import en.edu.zucc.pb.loginapplication.util.ResUtil;


public class KeyboardActivity extends AppCompatActivity implements KeyBoardLinearLayout.KeyBoartTouchListener {
    private static final String TAG = "HomeActivity";
    KeyBoardLinearLayout keyBoardLinearLayout;
    Button btn_back_to_login;

    private static final int REQUEST_CALL_PHONE = 1;

    public void doStartDial(View view, String phoneNum) {
        int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);

        if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{
                            Manifest.permission.CALL_PHONE},
                    REQUEST_CALL_PHONE);
            return;
        } else {
//            callPhone();
            callPhone(phoneNum);
        }

    }

    public void doStartDial(View view) {
        doStartDial(view, "");
    }

    private void callPhone() {
//        Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:xxxxx"));
//        Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:xxxxx"));
//        startActivity(intent);
        callPhone("xxxxx");
    }

    private void callPhone(String phoneNum) {
//        Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:xxxxx"));
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(String.format("tel:%s", phoneNum)));
        startActivity(intent);
    }

//    public void startNewActivity(View view) {
//        Intent intent = new Intent(this, AnotherActivity.class);
//        startActivity(intent);
//    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CALL_PHONE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    callPhone();
                    callPhone(dialStr);
                } else {
                    // 授权失败！
                    Toast.makeText(this, "没有拨打权限", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }
    }

    TextView tv_keyboard_ok;
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    Button btn5;
    Button btn6;
    Button btn7;
    Button btn8;
    Button btn9;
    Button btn0;
//    Button btn1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.keyboard_linearlayout);
//        setContentView(R.layout.home_activity);
        setContentView(R.layout.keyboard_linearlayout_all);
        Log.e("tag", "finished");
//        keyBoardLinearLayout = (KeyBoardLinearLayout) findViewById(R.id.kbll_keyboard);
//        keyBoardLinearLayout.setKeyBoartTouchListener(this);

//————————————————
//        版权声明：本文为CSDN博主「qq_36968707_白云」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//        原文链接：https://blog.csdn.net/qq_36968707/article/details/81163156

//        btn_back_to_login = findViewById(R.id.btn_back_to_login);
//        btn_back_to_login.setOnClickListener(new Button.OnClickListener() {
//            public void onClick(View v) { //使用匿名的Button实例
//
//                //抽象接口的内部方法的实现
//                Intent intent = new Intent(HomeActivity.this, MainActivity.class);
//                startActivity(intent);
//            }
//        });
//
//        tv_keyboard_ok = findViewById(R.id.tv_keyboard_ok);
//        tv_keyboard_ok.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
////                callPhone();
////                doStartDial(HomeActivity.this);
//                doStartDial(tv_keyboard_ok);
//            }
//        });

//        bindGridKeyboard();

//        tv_dial_str = findViewById(R.id.tv_dial_str);
//        btn1 = findViewById(R.id.btn1);
//        btn1.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                dialStr += btn1.getText();
//                tv_dial_str.setText(dialStr);
//            }
//        });
////        R.id. 可以字符串 表示
////        https://blog.csdn.net/jimbo_lee/article/details/23597083
//        String packageName = getPackageName();
//        Resources resources=getResources();
//        for (int i = 0; i < dialBtns.length; i++) {
////            Resources resources=getResources();
//            String btnIdStr = "btn"+(i+1);
////            int resID = resources.getIdentifier(btnIdStr, "drawable", getPackageName());
//            int resID = ResUtil.getResID(resources, btnIdStr, packageName);
//            Log.i(TAG, "image to show: "+btnIdStr + " id="+resID);
//
//            dialBtns[i] = findViewById(resID);
////            Button btn=dialBtns[i];
//            dialBtns[i].setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
////                    dialStr +=  getText();
//                    dialStr +=  ((Button)v).getText();
//                    tv_dial_str.setText(dialStr);
//                }
//
////                public void onClick(Button v) {
//////                    dialStr +=  this.getText();
//////                    HomeActivity.this.btn.getText();
////                    dialStr +=  getText();
////                    dialStr +=  v.getText();
////                    tv_dial_str.setText(dialStr);
////                }
//            });
//        }
//
//        btn_dial = findViewById(R.id.btn_dial);
//        btn_dial.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
////                callPhone();
////                doStartDial(HomeActivity.this);
//                doStartDial(btn_dial, dialStr);
//            }
//        });

        bindLinearlayoutAll();
    }

    void bindGridKeyboard(){

        keyBoardLinearLayout = (KeyBoardLinearLayout) findViewById(R.id.kbll_keyboard);
        keyBoardLinearLayout.setKeyBoartTouchListener(this);

        btn_back_to_login = findViewById(R.id.btn_back_to_login);
        btn_back_to_login.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) { //使用匿名的Button实例

                //抽象接口的内部方法的实现
                Intent intent = new Intent(KeyboardActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        tv_keyboard_ok = findViewById(R.id.tv_keyboard_ok);
        tv_keyboard_ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                callPhone();
//                doStartDial(HomeActivity.this);
                doStartDial(tv_keyboard_ok);
            }
        });
    }
    void bindLinearlayoutAll(){

        btn_back_to_login = findViewById(R.id.btn_back_to_login);
        btn_back_to_login.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) { //使用匿名的Button实例

                //抽象接口的内部方法的实现
                Intent intent = new Intent(KeyboardActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

//        tv_keyboard_ok = findViewById(R.id.tv_keyboard_ok);
//        tv_keyboard_ok.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
////                callPhone();
////                doStartDial(HomeActivity.this);
//                doStartDial(tv_keyboard_ok);
//            }
//        });

        tv_dial_str = findViewById(R.id.tv_dial_str);

//        btn1 = findViewById(R.id.btn1);
//        btn1.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                dialStr += btn1.getText();
//                tv_dial_str.setText(dialStr);
//            }
//        });

//        R.id. 可以字符串 表示
//        https://blog.csdn.net/jimbo_lee/article/details/23597083
//        String packageName = getPackageName();
//        Resources resources=getResources();
//        for (int i = 0; i < dialBtns.length; i++) {
////            Resources resources=getResources();
//            String btnIdStr = "btn"+(i+1);
////            https://www.cnblogs.com/spring87/p/4305635.html
////            int resID = resources.getIdentifier(btnIdStr, "drawable", getPackageName());
//            int resID = ResUtil.getResID(resources, btnIdStr, packageName);
//            Log.i(TAG, "image to show: "+btnIdStr + " id="+resID);
////            I/HomeActivity: image to show: btn1 id=0
////            貌似没有搜寻到
//
//            dialBtns[i] = findViewById(resID);
////            Button btn=dialBtns[i];
//            dialBtns[i].setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
////                    dialStr +=  getText();
//                    dialStr +=  ((Button)v).getText();
//                    tv_dial_str.setText(dialStr);
//                }
//
////                public void onClick(Button v) {
//////                    dialStr +=  this.getText();
//////                    HomeActivity.this.btn.getText();
////                    dialStr +=  getText();
////                    dialStr +=  v.getText();
////                    tv_dial_str.setText(dialStr);
////                }
//            });
//        }

//        bindDialBtns();
        bindDialBtnsSplitKind();

        btn_dial = findViewById(R.id.btn_dial);
        btn_dial.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                callPhone();
//                doStartDial(HomeActivity.this);
                doStartDial(btn_dial, dialStr);
            }
        });

        btn_clear= findViewById(R.id.btn_clear);
        btn_clear.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
             dialStr="";
                tv_dial_str.setText(dialStr);
            }
        });
        btn_back_one= findViewById(R.id.btn_back_one);
        btn_back_one.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                dialStr=dialStr.;
//                java 删除 字符串 最后一个
                if(dialStr.length()>1){
                    dialStr = dialStr.substring(0,dialStr.length() - 1);
                }

                tv_dial_str.setText(dialStr);
            }
        });
    }

    @Deprecated
    void setDialStrAndShow(String dialStr){
      this.  dialStr=dialStr;

    }
    Button    btn_clear;
    Button    btn_back_one;
    Button [] dialBtns=new Button[10];

    String dialStr = "";

    //    @BindView(R2.id.unloggin_layout)
    TextView tv_dial_str;
    Button btn_dial;

//    貌似失败了
    void bindDialBtns(){
        String packageName = getPackageName();
        Resources resources=getResources();
        for (int i = 0; i < dialBtns.length; i++) {
//            Resources resources=getResources();
            String btnIdStr = "btn"+(i+1);
//            https://www.cnblogs.com/spring87/p/4305635.html
//            int resID = resources.getIdentifier(btnIdStr, "drawable", getPackageName());
            int resID = ResUtil.getResID(resources, btnIdStr, packageName);
            Log.i(TAG, "image to show: "+btnIdStr + " id="+resID);
//            I/HomeActivity: image to show: btn1 id=0
//            貌似没有搜寻到

            dialBtns[i] = findViewById(resID);
//            Button btn=dialBtns[i];
            dialBtns[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    dialStr +=  getText();
                    dialStr +=  ((Button)v).getText();
                    tv_dial_str.setText(dialStr);
                }

//                public void onClick(Button v) {
////                    dialStr +=  this.getText();
////                    HomeActivity.this.btn.getText();
//                    dialStr +=  getText();
//                    dialStr +=  v.getText();
//                    tv_dial_str.setText(dialStr);
//                }
            });
        }
    }

    List<Button>dialBtnsLst=new ArrayList<>();

    void bindDialBtnsSplitKind(){
//        R.id.class.

        dialBtnsLst.add( (Button)findViewById(R.id.btn0));
        dialBtnsLst.add( (Button)findViewById(R.id.btn1));
        dialBtnsLst.add((Button) findViewById(R.id.btn2));
        dialBtnsLst.add( (Button)findViewById(R.id.btn3));
        dialBtnsLst.add( (Button)findViewById(R.id.btn4));
        dialBtnsLst.add( (Button)findViewById(R.id.btn5));
        dialBtnsLst.add( (Button)findViewById(R.id.btn6));
        dialBtnsLst.add( (Button)findViewById(R.id.btn7));
        dialBtnsLst.add( (Button)findViewById(R.id.btn8));
        dialBtnsLst.add( (Button)findViewById(R.id.btn9));
        for (Button button : dialBtnsLst) {
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    dialStr +=((Button) v).getText();
                    tv_dial_str.setText(dialStr);
                }
            });
        }


//                btn1 = findViewById(R.id.btn1);
//        btn1.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                dialStr += btn1.getText();
//                tv_dial_str.setText(dialStr);
//            }
//        });
//
//        btn2 = findViewById(R.id.btn2);
//        btn2.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                dialStr += btn2.getText();
//                tv_dial_str.setText(dialStr);
//            }
//        });

    }


    @Override
    public void onNumberTouch(String inputNumber) {
//        这个反应感觉很慢啊
        Toast.makeText(KeyboardActivity.this, "我点击了数字:" + inputNumber, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEmptyTouch() {
        Toast.makeText(KeyboardActivity.this, "我点击了清空按钮", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onOkTouch() {
        Toast.makeText(KeyboardActivity.this, "我点击了确定按钮", Toast.LENGTH_SHORT).show();
    }
//————————————————
//    版权声明：本文为CSDN博主「qq_36968707_白云」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//    原文链接：https://blog.csdn.net/qq_36968707/article/details/81163156    }
}
